﻿function TwitterStudiesService(dialogID, tableID) {
    var _dialog = $('#' + dialogID);
    _dialog.dialog({
        autoOpen: false,
        height: 350,
        width: 500,
        modal: true,
        title: 'Twitter Study Details',
        buttons: {
            'Save': function () {
                var obj = {};
                obj.ID = parseInt($('#StudyID').val());
                obj.Name = $('#StudyName').val();
                obj.Status = $('#Status').val();
                obj.Start = $('#StartDate').val();
                obj.End = $('#EndDate').val();
                window.PageBus.publish('table.save.' + tableID, obj);
            },
            'Close': function () {
                _dialog.dialog("close");
            }
        }
    });
    this.dialog = _dialog;
    window.PageBus.subscribe('table.add.' + tableID + '.done', this, this.addDone, null);
    $('.datepicker').datepicker({ dateFormat: 'm/d/yy' });
}
TwitterStudiesService.prototype.addDone = function (subj, addInput, data) {
    $("#" + addInput).val("");
};
TwitterStudiesService.prototype.add = function (addInput, callback) {
    var studyName = $('#' + addInput).val();
    studyName = $.trim(studyName);
    if (studyName.length == 0)
        return;
    Service.post("/TwitterStudy/Add/" + studyName, null, callback);
};
TwitterStudiesService.prototype.drill = function (values) {
    window.location.href = "/Twitter/Study/" + values[0];
};
TwitterStudiesService.prototype.edit = function (values) {
    $('#StudyID').val(values[0]);
    $('#StudyName').val(values[1]);
    $("#Status").val(values[2]);
    $("#StartDate").val(values[3]);
    $("#EndDate").val(values[4]);
    this.dialog.dialog("open");
};
TwitterStudiesService.prototype.remove = function (values, callback) {
    Service.post("/TwitterStudy/Remove/" + values[1], null, callback);
};
TwitterStudiesService.prototype.save = function (obj, callback) {
    var _dialog = this.dialog;
    function myCallback(result) {
        _dialog.dialog("close");
        callback(result);
    }
    Service.postJson("/TwitterStudy/Save", obj, myCallback);
};
function TwitterStudyService(dialogID, tableID) {
    var _dialog = $('#' + dialogID);
    _dialog.dialog({
        autoOpen: false,
        height: 350,
        width: 500,
        modal: true,
        title: 'Twitter Study Details',
        buttons: {
            'Save': function () {
                var obj = {};
                obj.ID = parseInt($('#StudyID').val());
                obj.Name = $('#StudyName').val();
                obj.Status = $('#Status').val();
                obj.Start = $('#StartDate').val();
                obj.End = $('#EndDate').val();
                window.PageBus.publish('table.save.' + tableID, obj);
            },
            'Close': function () {
                _dialog.dialog("close");
            }
        }
    });
    this.dialog = _dialog;
    window.PageBus.subscribe('table.add.' + tableID + '.done', this, this.addDone, null);
    $('.datepicker').datepicker({dateFormat: 'm/d/yy'});
}
TwitterStudyService.prototype.addDone = function (subj, addInput, data) {
    $("#" + addInput).val("");
};
TwitterStudyService.prototype.add = function (addInput, callback) {
    var studyName = $('#' + addInput).val();
    studyName = $.trim(studyName);
    if (studyName.length == 0)
        return;
    Service.post("/TwitterStudy/Add/" + studyName, null, callback);
};
TwitterStudyService.prototype.drill = function (values) {
    window.location.href = "/Twitter/Study/" + values[0];
};
TwitterStudyService.prototype.edit = function (values) {
    $('#StudyID').val(values[0]);
    $('#StudyName').val(values[1]);
    $("#Status").val(values[2]);
    $("#StartDate").val(values[3]);
    $("#EndDate").val(values[4]); 
    this.dialog.dialog("open");
};
TwitterStudyService.prototype.remove = function (values, callback) {
    Service.post("/TwitterStudy/Remove/" + values[1], null, callback);
};
TwitterStudyService.prototype.save = function (obj, callback) {
    var _dialog = this.dialog;
    function myCallback(result) {
        _dialog.dialog("close");
        callback(result);
    }
    Service.postJson("/TwitterStudy/Save", obj, myCallback);
};

function TwitterStudyUserService(studyID, dialogID, tableID) {
    this.studyID = studyID; this.tableID = tableID;
    var _dialog = $('#' + dialogID);
    _dialog.dialog({
        autoOpen: false,
        height: 350,
        width: 500,
        modal: true,
        title: 'Twitter User Details',
        buttons: {
            'Save': function () {
                var obj = {};
                obj.ID = $('#UserID').val();
                obj.Name = $('#UserName').val();
                obj.IsBlog = ($(":radio[name='IsBlog']:checked").val() == 'true');
                obj.IsNewspaper = ($(":radio[name='IsNewspaper']:checked").val() == 'true');
                obj.IsTopJournalist = ($(":radio[name='IsTopJournalist']:checked").val() == 'true');
                obj.IsTopPolitical = ($(":radio[name='IsTopPolitical']:checked").val() == 'true');
                window.PageBus.publish('table.save.' + tableID, obj);
            },
            'Close': function () {
                _dialog.dialog("close");
            }
        }
    });
    this.dialog = _dialog;
}
TwitterStudyUserService.prototype.studyID = null;
TwitterStudyUserService.prototype.tableID = null;
//TwitterStudyUserService.prototype.add = function (addInput, callback) {
//    var username = $('#' + addInput).val();
//    username = $.trim(username);
//    if (username.length == 0)
//        return;
//    var tableID = this.tableID;
//    function myCallback(results) {
//        window.PageBus.publish('table.add.' + tableID + '.added', addInput);
//        callback(results);
//    }

//    Service.post("/TwitterStudy/AddUser/" + this.studyID + '/' + username, null, myCallback);
//};
TwitterStudyUserService.prototype.remove = function (values, callback) {
    Service.post("/TwitterStudy/RemoveUser/" + this.studyID + '/' + values[1], null, callback);
};

TwitterStudyUserService.prototype.drill = function (values) {
    window.location.href = "/Twitter/Tweets/" + values[1];
};
TwitterStudyUserService.prototype.edit = function (values) {
    $('#UserID').val(values[0]);
    $('#UserName').val(values[1]);
    $("#IsBlog-" + values[2]).attr('checked', 'checked');
    $("#IsNewspaper-" + values[3]).attr('checked', 'checked');
    $("#IsTopJournalist-" + values[4]).attr('checked', 'checked');
    $("#IsTopPolitical-" + values[5]).attr('checked', 'checked');
    iconify();
    this.dialog.dialog("open");
};
TwitterStudyUserService.prototype.save = function (obj, callback) {
    var _dialog = this.dialog;
    function myCallback(result) {
        _dialog.dialog("close");
        callback(result);
    }
    Service.postJson("/TwitterUser/Save", obj, myCallback);
};
TwitterStudyUserService.prototype.saveStudy = function (obj, callback) {
    function myCallback(result) {
        callback(result);
    }
    Service.postJson("/TwitterStudy/Save", obj, myCallback);
};



function TwitterUserService(dialogID, tableID) {
    var _dialog = $('#' + dialogID);
    _dialog.dialog({
        autoOpen: false,
        height: 350,
        width: 500,
        modal: true,
        title: 'Twitter User Details',
        buttons: {
            'Save': function () {
                var obj = {};
                obj.ID = $('#UserID').val();
                obj.Name = $('#UserName').val();
                obj.IsBlog = ($(":radio[name='IsBlog']:checked").val() == 'true');
                obj.IsNewspaper = ($(":radio[name='IsNewspaper']:checked").val() == 'true');
                obj.IsTopJournalist = ($(":radio[name='IsTopJournalist']:checked").val() == 'true');
                obj.IsTopPolitical = ($(":radio[name='IsTopPolitical']:checked").val() == 'true');
                window.PageBus.publish('table.save.' + tableID, obj);
            },
            'Close': function () {
                _dialog.dialog("close");
            }
        }
    });
    this.dialog = _dialog;
    $('.datepicker').datepicker({ dateFormat: 'm/d/yy' });
}
TwitterUserService.prototype.add = function (addInput, callback) {
    var $input = $("#" + addInput);
    var username = $input.val();
    username = $.trim(username);
    if (username.length == 0)
        return;
    function myCallback(result) {
        $input.val("");
        callback(result);
    }
    Service.post("/TwitterUser/Add/" + username, null, myCallback);
};

TwitterUserService.prototype.drill = function (values) {
    window.location.href = "/Twitter/Tweets/" + values[1];
};
TwitterUserService.prototype.edit = function (values) {
    $('#UserID').val(values[0]);
    $('#UserName').val(values[1]);
    $("#IsBlog-" + values[2]).attr('checked', 'checked');
    $("#IsNewspaper-" + values[3]).attr('checked', 'checked');
    $("#IsTopJournalist-" + values[4]).attr('checked', 'checked');
    $("#IsTopPolitical-" + values[5]).attr('checked', 'checked');
    iconify();
    this.dialog.dialog("open");
};
TwitterUserService.prototype.remove = function (values, callback) {
    Service.post("/TwitterUser/Remove/" + values[1], null, callback);
};
TwitterUserService.prototype.save = function (obj, callback) {
    var _dialog = this.dialog;
    function myCallback(result) {
        _dialog.dialog("close");
        callback(result);
    }
    Service.postJson("/TwitterUser/Save", obj, myCallback);
};
TwitterUserService.prototype.load = function (obj, callback) {
    function myCallback(result) {
        callback(result);
    }
    var model = {};
    model.IDs = [];
    model.IDs.push(obj[0]);
    model.EndDate = $('#endDate').val();
    Service.postJson("/TwitterUser/Load", model, myCallback);
};






function TweetsHandler() {
    this.$dialog = $("#tweetDetails");
    this.$dialog.dialog({
        autoOpen: false,
        height: 500,
        width: 1050,
        modal: true,
        title: 'Tweet',
        buttons: {
            'Save': function () {
                window.PageBus.publish(TweetsHandler.save);
            },
            'Close': function () {
                window.PageBus.publish(TweetsHandler.close);
            },
            'Previous': function () {
                window.PageBus.publish(TweetsHandler.previous);
            },
            'Next': function () {
                window.PageBus.publish(TweetsHandler.next);
            }
        }
    });
    window.PageBus.subscribe(TweetsHandler.open, this, this.openHandler, this.$dialog);
    window.PageBus.subscribe(TweetsHandler.close, this, this.closeHandler, this.$dialog);
    window.PageBus.subscribe(TweetsHandler.save, this, this.saveHandler, this.$dialog);
    window.PageBus.subscribe(TweetsHandler.next, this, this.nextHandler, this.$dialog);
    window.PageBus.subscribe(TweetsHandler.previous, this, this.previousHandler, this.$dialog);
}
TweetsHandler.prototype.service = null;
TweetsHandler.open = "tweets.dialog.open";
TweetsHandler.close = "tweets.dialog.close";
TweetsHandler.save = "tweets.dialog.save";
TweetsHandler.next = "tweets.dialog.next";
TweetsHandler.previous = "tweets.dialog.previous";

TweetsHandler.prototype.nextHandler = function (subj, msg, popup) {
    $(".picklist span.selected").parent().next().find("span").click();
};
TweetsHandler.prototype.previousHandler = function (subj, msg, popup) {
    $(".picklist span.selected").parent().prev().find("span").click();
};
TweetsHandler.prototype.openHandler = function (subj, msg, popup) {
    $(".picklist span").removeClass('selected');
    $(msg.source).addClass('selected');
    function callback(results) {
        popup.html(results);
        //$("#tabs").tabs();
        //$("#tweetAttributes").buttonset();
        $(":checkbox").button();
        $(".toneList").buttonset();
        $(".buttonset").buttonset();
        $('.Positive').button({ text: false, icons: { primary: 'ui-icon-plus'} })
        $('.Negative').button({ text: false, icons: { primary: 'ui-icon-minus'} })
        $('.Neutral').button({ text: false, icons: { primary: 'ui-icon-radio-on'} })
        popup.dialog("open");
    }
    Service.get("/Twitter/Tweet/" + msg.id, null, callback, "html");

};
TweetsHandler.prototype.closeHandler = function (subj, msg, popup) {
    popup.dialog("close");
};
TweetsHandler.prototype.saveHandler = function (subj, msg, popup) {
    var tweet = {};
    tweet.Identifier = $("#tweetID").val();
    tweet.IsClarification = ($("#IsClarification").attr("checked")) ? true : false;
    tweet.IsGeneral = ($("#IsGeneral").attr("checked")) ? true : false;
    tweet.IsQuestion = ($("#IsQuestion").attr("checked")) ? true : false;
    tweet.Genre = $("#tweetGenre").val();
    tweet.Links = [];
    tweet.Mentions = [];
    var links = $(".link");
    for (var i = 0; i < links.length; i++) {
        var id = parseInt(links[i].innerHTML);
        var isInternal = ($("#IsInternal-" + id).attr("checked")) ? true : false;
        var link = {
            Sequence: id,
            Tone: $("input[name='tweetLinkTone-" + id + "']:checked").val(),
            IsInternal: isInternal
        };
        tweet.Links.push(link);
    }
    var mentions = $(".mention");
    for (var i = 0; i < mentions.length; i++) {
        var id = parseInt(mentions[i].innerHTML);
        var isVia = ($("#IsVia-" + id).attr("checked")) ? true : false;
        var isRetweet = ($("#IsRetweet-" + id).attr("checked")) ? true : false;
        var mention = {
            Sequence: id,
            IsRetweet: isRetweet,
            IsVia: isVia
        };
        tweet.Mentions.push(mention);
    }

    var status = $("input[name='Status']:checked").val();
    tweet.Status = (status && status.length > 0) ? status : "Active";

    function callback(result) {
        //popup.dialog("close");
        notify.info("Saved tweet: '"+ $('#tweetText').text() +"'", "Saved");
    }

    Service.postJson("/Twitter/SaveTweet", tweet, callback);
};